package com.fiveGroup.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fiveGroup.pojo.vo.DataVo;
import com.fiveGroup.pojo.vo.MessageVo;
import com.fiveGroup.service.MessageService;
import com.fiveGroup.service.MessageVoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/comment")
@Slf4j
public class BackgroundCommentManage {

    @Autowired
    private MessageService messageService;


    @Autowired
    private MessageVoService messageVoService;

    /**
     * 获取所有消息
     */
    @GetMapping("/getAllMessages")
    public DataVo<IPage<MessageVo>> getAllMessages(
            @RequestParam(required = false) Long senderId,
            @RequestParam(required = false) Long receiverId,
            @RequestParam(required = false) String msgType,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize
    ) {

        // 创建分页对象
        IPage<?> page = new Page<>(currentPage, pageSize);

        // 调用 Service 层的分页查询方法
        IPage<MessageVo> messageIPage = messageVoService.getMessageListPage(page, senderId, receiverId, msgType);

        // 将分页结果封装到 DataVo 中返回
        return DataVo.success(messageIPage);
    }

    /**
     * 编辑消息
     */
    @PutMapping("/editMessage/{id}")
    public DataVo editMessage(@PathVariable Long id, @RequestBody Map<String, String> request) {
        String content = request.get("content");
        if (id == null || StringUtils.isEmpty(content)) {
            return DataVo.fail("编辑失败！");
        }

        try {
            boolean success = messageService.editMessage(id, content);
            if (success) {
                return DataVo.success("消息编辑成功");
            } else {
                return DataVo.fail("消息编辑失败");
            }
        } catch (IllegalArgumentException e) {
            log.error("编辑消息失败: Invalid argument", e);
            return DataVo.fail("编辑消息失败: " + e.getMessage());
        } catch (Exception e) {
            log.error("编辑消息失败:", e);
            return DataVo.fail("编辑消息失败");
        }
    }

    /**
     * 删除消息
     */
    @DeleteMapping("/deleteMessage/{id}")
    public DataVo deleteMessage(@PathVariable Long id) {
        if (id == null) {
            return DataVo.fail("Invalid parameters");
        }

        try {
            boolean success = messageService.deleteMessage(id);
            if (success) {
                return DataVo.success("消息删除成功");
            } else {
                return DataVo.fail("消息删除失败");
            }
        } catch (IllegalArgumentException e) {
            log.error("删除消息失败: Invalid argument", e);
            return DataVo.fail("删除消息失败: " + e.getMessage());
        } catch (Exception e) {
            log.error("删除消息失败:", e);
            return DataVo.fail("删除消息失败");
        }
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/markAsRead/{id}")
    public DataVo markAsRead(@PathVariable Long id) {
        if (id == null) {
            return DataVo.fail("标记失败");
        }

        try {
            boolean success = messageService.markAsRead(id);
            if (success) {
                return DataVo.success("消息已标记为已读");
            } else {
                return DataVo.fail("标记消息为已读失败");
            }
        } catch (IllegalArgumentException e) {
            log.error("标记消息为已读失败: Invalid argument", e);
            return DataVo.fail("标记消息为已读失败: " + e.getMessage());
        } catch (Exception e) {
            log.error("标记消息为已读失败:", e);
            return DataVo.fail("标记消息为已读失败");
        }
    }

    /**
     * 批量删除消息
     */
    @PostMapping("/batchDeleteMessages")
    public ResponseEntity<DataVo> batchDeleteMessages(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return ResponseEntity.badRequest().body(DataVo.fail("Invalid parameters"));
        }

        try {
            boolean success = messageService.batchDeleteMessages(ids);
            if (success) {
                return ResponseEntity.ok(DataVo.success("批量删除成功"));
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(DataVo.fail("批量删除失败"));
            }
        } catch (IllegalArgumentException e) {
            log.error("批量删除消息失败: Invalid argument", e);
            return ResponseEntity.badRequest().body(DataVo.fail("批量删除消息失败: " + e.getMessage()));
        } catch (Exception e) {
            log.error("批量删除消息失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(DataVo.fail("批量删除失败"));
        }
    }

    /**
     * 批量标记消息为已读
     */
    @PostMapping("/batchMarkAsRead")
    public ResponseEntity<DataVo> batchMarkAsRead(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return ResponseEntity.badRequest().body(DataVo.fail("Invalid parameters"));
        }

        try {
            boolean success = messageService.batchMarkAsRead(ids);
            if (success) {
                return ResponseEntity.ok(DataVo.success("批量标记为已读成功"));
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(DataVo.fail("批量标记为已读失败"));
            }
        } catch (IllegalArgumentException e) {
            log.error("批量标记消息为已读失败: Invalid argument", e);
            return ResponseEntity.badRequest().body(DataVo.fail("批量标记消息为已读失败: " + e.getMessage()));
        } catch (Exception e) {
            log.error("批量标记消息为已读失败:", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(DataVo.fail("批量标记为已读失败"));
        }
    }
}